<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展 
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------

namespace app\admin\model\wddj;

use app\admin\model\wddj\user\Passenger;
use app\admin\model\wddj\user\UserRealNameAuthentication;
use app\admin\model\wddj\user\Wechat;
use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'wddj_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'departure_time_text',
        'travel_time_text',
        'paid_text',
        'pay_time',
        'pay_time_text',
        'pay_method_text',
        'state_text',
        'refund_time_text',
        'payer'
    ];
    

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getPaidList()
    {
        return ['1' => __('Paid 1'), '2' => __('Paid 2'), '3' => __('Paid 3')];
    }

    public function getPayMethodList()
    {
        return ['1' => __('Pay_method 1')];
    }

    public function getStateList()
    {
        return ['pending' => __('State pending'), 'accepted' => __('State accepted'), 'in_progress' => __('State in_progress'), 'awaiting_payment' => __('State awaiting_payment'), 'completed' => __('State completed'), 'cancelled' => __('State cancelled')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDepartureTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['departure_time']) ? $data['departure_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTravelTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['travel_time']) ? $data['travel_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPaidTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paid']) ? $data['paid'] : '');
        $list = $this->getPaidList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeAttr($value, $data)
    {
        $paymentsObj = (new Payments())
            ->where('order_id',$data['id'])
            ->where('payment_type','prepaid')
            ->where('payment_status','completed')
            ->find();
        return $paymentsObj ? $paymentsObj['pay_time'] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $paymentsObj = (new Payments())
            ->where('order_id',$data['id'])
            ->where('payment_type','prepaid')
            ->where('payment_status','completed')
            ->find();
        return $paymentsObj ? $paymentsObj['pay_time'] : '';
    }


    public function getPayMethodTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_method']) ? $data['pay_method'] : '');
        $list = $this->getPayMethodList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_time']) ? $data['refund_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setDepartureTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTravelTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRefundTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function agent()
    {
        return $this->belongsTo('app\admin\model\wddj\CertifiedDrivingAgent', 'driver_wechat_id', 'wechat_id', [], 'LEFT')->setEagerlyType(1);
    }

    /**
     * Desc 订单详情获取乘车人信息
     * Create on 2024/11/5 9:14
     * Create by wangyafang
     */
    public static function getPassenger($orderObj)
    {
        if (!empty($orderObj['passenger_id'])) {
            $passengerObj = (new Passenger())->get($orderObj['passenger_id']);
            $data = array(
                'name'=>$passengerObj['name'],
                'mobile'=>$passengerObj['mobile'],
            );
        } else {
            $userRealNameAuthenticationName = (new UserRealNameAuthentication())->where('wechat_id',$orderObj['wechat_id'])->value('name');
            $userWechatObj = (new Wechat())->get($orderObj['wechat_id']);
            $data = array(
                'name'=>!empty($userRealNameAuthenticationName) ? $userRealNameAuthenticationName : $userWechatObj['nickname'],
                'mobile'=>$userWechatObj['mobile'],
            );
        }

        return $data;

    }

    public function getActualMeterAttr($value)
    {
        return bcdiv($value,1000,2);
    }

    public function getAdjustAmountAttr($value)
    {
        if ($value > 0) {
            $value = '多退'.$value;
        }
        if ($value < 0) {
            $value = '少补'.abs($value);
        }
        return $value;
    }

    public function getPayerAttr($value, $data)
    {
        $name = (new UserRealNameAuthentication())->where('wechat_id',$data['id'])->value('name');
        if (!$name) {
            $userWechatObj = (new Wechat())->where('id',$data['wechat_id'])->field('nickname,mobile')->find();
            $name = $userWechatObj['nickname'];
            $mobile = $userWechatObj['mobile'];
        } else {
            $mobile = (new Wechat())->where('id',$data['wechat_id'])->value('mobile');
        }

        $data = array(
            'name'=>$name,
            'mobile'=>$mobile,
        );
        return $data;
    }
}
